package com.kakame.codenews.front.message.dao;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.kakame.codenews.base.constant.SystemConstant;
import com.kakame.codenews.base.util.SystemUtil;
import com.kakame.codenews.base.web.dao.JdbcBaseDao;
import com.kakame.codenews.base.web.entity.Page;
import com.kakame.codenews.base.web.fast.mapping.nexus.ArticleComment;
import com.kakame.codenews.base.web.fast.mapping.nexus.UserMessage;

@Repository
public class FrontMessageDao extends JdbcBaseDao {

    public Integer countNoReadMessage(String userId) {
        Map<String, Object> paramMap = SystemUtil.getSOHashMap();
        paramMap.put("userId", userId);
        String sql = "";
        sql += "SELECT " + SystemConstant.SINGLE_TABLE_QUERY_ALL_COLUMN;
        sql += "  FROM t_nexus_user_message ";
        sql += " WHERE message_status = '0' ";
        sql += "       AND status = 'C' ";
        sql += "       AND to_id = :userId ";
        List<UserMessage> list = list(sql, paramMap, UserMessage.class);
        return list.size();
    }

    public void insertNoReadMessage(List<UserMessage> userMessages) {
        super.batchInsert(userMessages);
    }

    public Page<ArticleComment> messageGrid(Map<String, Object> queryMap) {
        String sql = "";
        sql += "SELECT nac.id, nac.comment_content, nac.create_time ";
        sql += "  FROM t_nexus_user_message num ";
        sql += "  LEFT JOIN t_nexus_article_comment nac ON num.message_id = nac.id";
        sql += " WHERE num.message_status = '0' ";
        sql += "       AND num.status = 'C' ";
        sql += "       AND nac.status = 'C' ";
        sql += "       AND num.to_id = :userId ";
        sql += " ORDER BY nac.create_time DESC ";
        
        List<ArticleComment> countList = list(sql, queryMap, ArticleComment.class);
        sql += " LIMIT :pageStart, :pageSize ";
        
        List<ArticleComment> list = list(sql, queryMap, ArticleComment.class);
        Integer pageNo = (Integer) queryMap.get("pageNo");
        Integer pageSize = (Integer) queryMap.get("pageSize");
        Page<ArticleComment> page = new Page<ArticleComment>(list, pageNo, pageSize, countList.size());
        
        return page;
    }
    
}
